<template>
  <div v-loading="pageLoading" style="overflow-x: auto;">
    <!-- 控制按钮 -->
    <div class="mb_18">
      <el-button size="medium" type="primary" icon="el-icon-circle-plus-outline" @click="addTraining()">新增科目</el-button>
    </div>
    <!-- 页面列表 -->
    <div v-for="(item, index) in pageList" :key="index" class="flex_model">
      <div v-show="item.isOfficial" class="flex_official">官</div>
      <div class="flex_row">
        <div v-for="(jtem, jndex) in item.levels" :key="jndex" class="flex_col gl_clear">
          <div class="flex_col_title gl_fl">
            <span>{{ traningTxtUppercase[jtem.level - 1] }}级科目</span>
          </div>
          <div class="flex_col_content">
            <div v-for="(ntem, ndex) in jtem.templateParts" :key="ndex">{{ ntem.name }}</div>
          </div>
        </div>
      </div>
      <div class="flex_control">
        <div class="flex_control_mb">
          <el-button type="primary" style="width:120px" size="medium" @click="editTraining(item)">编辑</el-button>
        </div>
        <div>
          <el-button type="primary" style="width:120px" size="medium" @click="deleteTraining(item)">删除</el-button>
        </div>
      </div>
    </div>
    <!-- 弹窗 -->
    <trainingDialog ref="refTrainingDialog" subject-type-code="" @onSubmit="onSubmitTraining" />
  </div>
</template>

<script>
// 培训科目设置

import trainingDialog from './components/addTrainingDialog.vue';

import {
  api_GET_TrainSubjectTemplate, // 根据科目类型获取科目模板列表 培训 Train  考试 Exam
  api_POST_TrainSubjectTemplate, // 创建科目模板
  api_DELETE_TrainSubjectTemplateId, // 删除科目模板
  api_PUT_TrainSubjectTemplateId // 更新科目模板
} from '@/extend/api/trainswaggerApi/app.js';

export default {
  components: {
    trainingDialog
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      traningTxtUppercase: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
      pageLoading: false,
      pageList: [],
      dialogType: '',
      templateId: ''
    };
  },
  watch: {
    isShow: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getPageList();
        }
      }
    }
  },
  methods: {
    // 获取页面数据
    getPageList () {
      this.pageLoading = true;
      api_GET_TrainSubjectTemplate({
        subjectTypeCode: 'Train'
      })
        .then(
          res => {
            this.pageLoading = false;
            this.pageList = res;
          }
        )
        .catch(
          error => {
            this.pageLoading = false;
            this.$message({
              message: `科目获取失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    // 添加科目
    addTraining () {
      this.dialogType = 'NEW';
      this.$refs.refTrainingDialog.openDialog({
        type: 'NEW'
      });
    },
    editTraining (data) {
      this.dialogType = 'EDIT';
      this.templateId = data.templateId;
      const _formList = [];

      for (let i = 0; i < data.levels.length; i++) {
        const item = data.levels[i];
        const _o = {
          inputVisible: false,
          inputValue: '',
          children: []
        };
        for (let j = 0; j < item.templateParts.length; j++) {
          const jtem = item.templateParts[j];
          _o.children.push(jtem);
        }
        _formList.push(_o);
      }

      const _first = _formList[0].children[0].name;
      const _form = {
        first: _first,
        isOfficial: data.isOfficial,
        list: [
          ..._formList.slice(1)
        ]
      };

      this.$refs.refTrainingDialog.openDialog({
        type: 'EDIT',
        form: _form
      });
    },
    onSubmitTraining (data) {
      if (this.dialogType === 'NEW') {
        this.onSubmitAddTraining(data);
      }

      if (this.dialogType === 'EDIT') {
        this.onSubmitEditTraining(data);
      }
    },
    onSubmitEditTraining (data) {
      const _list = data.list;
      const _tplParts = [];
      for (let i = 0; i < _list.length; i++) {
        const item = _list[i];
        const _level = i + 2;
        for (let j = 0; j < item.children.length; j++) {
          const jtem = item.children[j];
          _tplParts.push({
            level: _level,
            name: jtem.name
          });
        }
      }

      const _templateParts = [
        {
          level: 1,
          name: data.first
        },
        ..._tplParts
      ];

      const vo = {
        'isOfficial': data.isOfficial,
        'templateParts': _templateParts,
        'subjectTypeCode': 'Train'
      };
      this.updateTrainingAPI(this.templateId, vo);
    },
    updateTrainingAPI (id, vo) {
      this.$refs.refTrainingDialog.changeSubmitLoading(true);
      api_PUT_TrainSubjectTemplateId(id, vo)
        .then(
          res => {
            this.$refs.refTrainingDialog.changeSubmitLoading(false);
            this.$refs.refTrainingDialog.closeDialog();
            this.getPageList();
            this.$message({
              message: `编辑科目成功`,
              type: 'success',
              showClose: true
            });
          }
        )
        .catch(
          error => {
            this.$refs.refTrainingDialog.changeSubmitLoading(false);
            this.$message({
              message: `编辑科目失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    onSubmitAddTraining (data) {
      const _list = data.list;
      const _tplParts = [];
      for (let i = 0; i < _list.length; i++) {
        const item = _list[i];
        const _level = i + 2;
        for (let j = 0; j < item.children.length; j++) {
          const jtem = item.children[j];
          _tplParts.push({
            level: _level,
            name: jtem.name
          });
        }
      }

      const _templateParts = [
        {
          level: 1,
          name: data.first
        },
        ..._tplParts
      ];

      const vo = {
        'isOfficial': data.isOfficial,
        'templateParts': _templateParts,
        'subjectTypeCode': 'Train'
      };
      this.addTrainingAPI(vo);
    },
    addTrainingAPI (vo) {
      this.$refs.refTrainingDialog.changeSubmitLoading(true);
      api_POST_TrainSubjectTemplate(vo)
        .then(
          res => {
            this.$refs.refTrainingDialog.changeSubmitLoading(false);
            this.$refs.refTrainingDialog.closeDialog();
            this.getPageList();
            this.$message({
              message: `新增科目成功`,
              type: 'success',
              showClose: true
            });
          }
        )
        .catch(
          error => {
            this.$refs.refTrainingDialog.changeSubmitLoading(false);
            this.$message({
              message: `新增科目失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    // 删除科目
    deleteTraining (item) {
      this.$confirm('您确定删除培训科目吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(
          () => {
            this.deleteTrainingAPI(item.templateId);
          }
        );
    },
    // 删除科目API
    deleteTrainingAPI (id) {
      api_DELETE_TrainSubjectTemplateId(id)
        .then(
          res => {
            this.$message({
              message: `删除成功`,
              type: 'success',
              showClose: true
            });
            this.getPageList();
          }
        )
        .catch(
          error => {
            this.$message({
              message: `科目删除失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    }
  }
};
</script>

<style lang="scss" scoped>
  .flex_model {
    position: relative;
    min-height: 138px;
    margin-bottom: 28px;
  }
  .flex_official {
    box-sizing: border-box;
    position: absolute;
    top: 50%;
    left: 0;
    width: 22px;
    height: 22px;
    margin-top: -11px;
    font-size: 12px;
    line-height: 22px;
    text-align: center;
    color: #1990ff;
    border: 1px solid #1990ff;
    border-radius: 50%;
    background-color: #fff;
  }
  .flex_row {
    box-sizing: border-box;
    width: 1400px;
    margin-left: 34px;
    min-height: 138px;
    display: flex;
    border-radius: 8px;
    border: 1px solid #d9d9d9;
    overflow: hidden;
  }
  .flex_col {
    flex: 1;
  }
  .flex_control {
    position: absolute;
    top: 50%;
    left: 1434px;
    margin-top: -64px;
    padding: 19px 0 19px 40px;
  }
  .flex_control_mb {
    margin-bottom: 18px;
  }
  .flex_col_title {
    box-sizing: border-box;
    width: 32px;
    padding: 0 9px;
    height: 100%;
    font-size: 14px;
    line-height: 18px;
    color: rgba(0, 0, 0, 0.65);
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #E8F4FF;
  }
  .flex_col_content {
    box-sizing: border-box;
    height: 100%;
    padding: 10px 0;
    margin-left: 55px;
    font-size: 14px;
    line-height: 18px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    color: rgba(0,0,0,0.65);
  }

  .mb_18 {
    margin-bottom: 18px;
  }
</style>
